package com.ruoyi.common.core.redis;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

//https://blog.csdn.net/captian_900331/article/details/130969749
@Component
public class RedissonConfig {
    private final Logger logger = LoggerFactory.getLogger(RedissonConfig.class);
    @Autowired
    private RedisProperties redisProperties;

    @Bean(destroyMethod = "shutdown")
    public RedissonClient redisson() {

        Config config = new Config();
        logger.info("RedissonConfig host {} port {} passwd {} ",redisProperties.getHost(),
                redisProperties.getPort(),redisProperties.getPassword());
//        config.useSingleServer().setAddress("XXX.XX.XX.X(redis地址):端口").setPassword("xxxxxxxxx");
        config.useSingleServer()
                .setAddress(String.format("redis://%s:%d",
                redisProperties.getHost(), redisProperties.getPort()))
                .setPassword(redisProperties.getPassword());
        return Redisson.create(config);
    }
}
